package com.bonc.ioc.controller;

import com.bonc.ioc.base.controller.BaseController;
import com.bonc.ioc.base.page.PageResult;
import com.bonc.ioc.common.util.AppReply;
import com.bonc.ioc.model.Demo;
import com.bonc.ioc.service.impl.DemoServiceImpl;
import com.bonc.ioc.validation.AppAuthAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 案例
 *
 * @author ycy
 * @date 2020/7/11 13:27
 */

@RestController
@RequestMapping("/demo")
public class DemoController extends BaseController {

    @Autowired
    private DemoServiceImpl demoService;

    /**
     * 分页查询数据
     *
     * @param parm1：参数1
     * @param parm2：参数2
     * @return AppReply
     * @throws Exception
     * @author ycy
     * @date 2020/7/11 13:24
     */
    @GetMapping("/selectDemosByPage")
    public AppReply selectDemosByPage(String parm1, String parm2) throws Exception {
        return new AppReply(AppReply.SUCCESS_CODE,"查询成功",demoService.selectDemosByPage(parm1, parm2));
    }

    /**
     * 保存数据
     *
     * @param demo：参数
     * @return AppReply
     * @throws Exception
     * @author ycy
     * @date 2020/7/11 13:24
     */
    @PostMapping("/save")
    public AppReply save(@Validated Demo demo) throws Exception {
        Integer count = demoService.save(demo);
        if(count > 0){
            return AppReply.success("保存成功");
        }
        return  AppReply.error("保存失败");
    }


    /**
     * 往redis里保存数据
     *
     * @author ycy
     * @date 2020/7/11 13:19
     * @param key
     * @param value
     * @return
     * @exception
     */
    @PostMapping("/setToRedis")
    public AppReply setToRedis(String key,String value) throws Exception{
        demoService.setToRedis(key,value);
        return AppReply.success("设置成功");
    }

    /**
     * 从redis里获取数据
     *
     * @author ycy
     * @date 2020/7/11 13:19
     * @param key
     * @return
     * @exception
     */
    @GetMapping("/getFromRedis")
    public AppReply getFromRedis(String key) throws Exception{
        return new AppReply(AppReply.SUCCESS_CODE,"查询成功",demoService.getFromRedis(key));
    }

}
